package GUI.view;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;

/**
 * 
 * @author Wei WANG
 * 
 */
public class FileManager extends JPanel implements ActionListener {

	public FileManager() {
		initPanel();
	}

	JPanel tablesPanel;
	JPanel tableAndConfPanel;
	XMLFileTreePanel xmlPanel;
	JPanel southPanel;

	public void initPanel() {
		this.setLayout(new BorderLayout());

		tableAndConfPanel = new JPanel();
		tableAndConfPanel.setLayout(new BorderLayout());

		tablesPanel = new JPanel();
		tablesPanel.setLayout(new BorderLayout());

		tableAndConfPanel.add(tablesPanel, BorderLayout.CENTER);

		xmlPanel = new XMLFileTreePanel(tablesPanel);
		tableAndConfPanel.add(xmlPanel, BorderLayout.SOUTH);

		JScrollPane scrollPane = new JScrollPane(
				MultiLineTableConstructor.createLogTable(xmlPanel
						.getXMLFileTree().getRootNode()));
		tablesPanel.add(scrollPane, BorderLayout.CENTER);

		add(tableAndConfPanel, BorderLayout.CENTER);

		southPanel = new JPanel();
		// southPanel.setLayout();
		JButton createButton = new JButton("Create");
		createButton.addActionListener(this);
		southPanel.add(createButton);
		add(southPanel, BorderLayout.SOUTH);
	}

	public void updateTablePanel(JTable table) {
		tablesPanel.removeAll();
		JScrollPane scrollPane = new JScrollPane(
				MultiLineTableConstructor.createLogTable(xmlPanel
						.getXMLFileTree().getRootNode()));
		tablesPanel.add(scrollPane, BorderLayout.CENTER);
		tablesPanel.updateUI();
	}

	private static void createAndShowGUI() {
		// Create and set up the window.
		JFrame frame = new JFrame("Demo");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		// Create and set up the content pane.
		JPanel newContentPane = new JPanel();
		newContentPane.setLayout(new BorderLayout());

		newContentPane.add(new FileManager(), BorderLayout.CENTER);
		// newContentPane.setOpaque(true); //content panes must be opaque
		frame.setContentPane(newContentPane);

		// Display the window.
		frame.pack();
		frame.setVisible(true);
	}

	public static void main(String[] args) {
		// Schedule a job for the event-dispatching thread:
		// creating and showing this application's GUI.
		javax.swing.SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				createAndShowGUI();
			}
		});
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		boolean b = xmlPanel.getXMLFileTree().getRootNode().verifyComplete();
		if (b) {
			String s = xmlPanel.getXMLFileTree().getRootNode()
					.getStringRepresentation();
			FileWriter fw = null;
			File f = new File(
					"C:\\Users\\NAJIB\\Documents\\NetBeansProjects\\WDT-2\\FileXMLCreateByWDT.xml");
			try {
				if (!f.exists()) {
					f.createNewFile();
				}
				fw = new FileWriter(f);
				BufferedWriter out = new BufferedWriter(fw);
				out.write(s, 0, s.length() - 1);
				out.close();
			} catch (IOException ec) {
				ec.printStackTrace();
			}
		} else {
			Object[] options = { "OK", "Cancel" };
			int n = JOptionPane.showOptionDialog(this,
					"Information incomplete!", "Information incomplete",
					JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE,
					null, // do not use a custom Icon
					options, // the titles of buttons
					options[0]); // default button title

		}

	}

}
